//导入篮子绘制模块
import Basket from './Basket.js';
//导入鸡蛋绘制模块
import Egg from './Egg.js';

//导入爆炸模块
import Boom from './Boom.js';
//导入分数模块
import Info from './Info.js';

//在水池中进行批量操作
class Pool{
  constructor(ctx, basket, egg,music) {
    this.ctx = ctx;
    this.music = music;
    this.basket = basket;
    this.egg = egg;
    this.eggs = [];
    this.booms = [];
    this.score = 0;
    //判断游戏是否结束（默认没有）
    this.isOver = false;
    this.top = 0;

    //鸡蛋碎掉的19张图片
    this.animation = [];

    for (let i = 1; i <= 19; i++) {
      let img = new Image();
      img.src = 'images/' + i + '.png';
      this.animation.push(img);
    }
  }
  toTop(){
    this.top += 1;
    if (this.top >= this.ctx.canvas.height) {
      //鸡蛋已经到了碎掉的区域
      this.top = 0;
    }
  }

  draweggs(){
    this.toTop();
    //批量产生鸡蛋
    if(this.top % 80 === 0){
      //垂直方向每移动70距离就产生一枚鸡蛋
      const egg = new Egg(this.ctx); 
      this.eggs.push(egg);
    }
     //过滤出可视区域的鸡蛋
     this.eggs = this.eggs.filter(item =>{
       return item.status === true;
     })
     //批量绘制鸡蛋
     this.eggs.forEach(egg =>{
       egg.draw();
       //判断鸡蛋是否掉在地上
       egg.judgeCatch(this.basket)
       if (egg.isCatch) {
         this.score += 1
       } else {
         if (egg.init.y + 21 >= this.basket.init.y + this.basket.init.h / 2) {
           egg.status = false
           let boom = new Boom(this.ctx, egg);
           this.booms.push(boom);
           //判断游戏是否结束（默认没有）
           this.isOver = true;
         }
       }
     })
   
  }
  drawBooms() {
    //绘制爆炸效果
    //批量绘制鸡蛋碎掉效果
    this.booms = this.booms.filter(item => {
      return item.status === true
    })
    this.booms.forEach(boom => {
      //产生鸡蛋碎掉声
      // this.music.boomPaly();
      boom.draw(this.animation);
    })
  }
  replay() {
    const replay = new Replay(this.ctx, this.score);
    replay.replay();
  }
}
export default Pool;